package com.lovo.psc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.psc.bean.OrderBean;
import com.lovo.psc.bean.OrderInfoBean;
import com.lovo.psc.bean.OrderProductBean;
import com.lovo.psc.mapper.IOrderInfoMapper;
import com.lovo.psc.mapper.IOrderMapper;
import com.lovo.psc.mapper.IOrderProductMapper;
import com.lovo.psc.service.IOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class IOrderInfoServiceImpl implements IOrderInfoService {

    @Autowired
    private IOrderInfoMapper mapper;
    @Autowired
    private IOrderProductMapper productMapper;
    @Autowired
    private IOrderMapper orderMapper;


    @Override
    public void add(OrderInfoBean order) {
        mapper.insert(order);
    }


    @Override
    public void updateStatusByOrderInfoIdNum(String idNum, int status) {
        QueryWrapper<OrderInfoBean> qw = new QueryWrapper<>();
        qw.eq("oi_idNum",idNum);
        OrderInfoBean or = mapper.selectOne(qw);
        or.setStatus(status);
        mapper.updateById(or);
        //判断为退货
        if (status==2){
            //查询退货订单
            QueryWrapper<OrderProductBean> qw2 = new QueryWrapper<>();
            qw2.eq("fk_ooId",or.getOrderInfoId());
            OrderProductBean orderInfoBean = productMapper.selectOne(qw2);

            //查询主订单
            QueryWrapper<OrderBean> qw3 = new QueryWrapper<>();
            qw3.eq("pk_orderId",or.getOrderIdNum());
            OrderBean orderBean = orderMapper.selectOne(qw3);

            //修改主订单的未供货数量和结算金额
            orderBean.setRest(orderBean.getRest()+orderInfoBean.getNum());
            orderBean.setGetMoney(orderBean.getGetMoney()-(orderInfoBean.getNum()*orderInfoBean.getBuyPrice()));
            orderMapper.updateById(orderBean);
        }
    }
}
